#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from fastapi import APIRouter, Depends
from utils.response import SuccessResponse
from . import schemas, crud, params
from core.dependencies import IdList
from apps.base.auth.validate import AllUserAuth, Auth


app = APIRouter()


@app.get("/plan", summary="获取计划待办")
async def get_plan(
        p: params.PlanParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.PlanDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.PlanSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/plan", summary="创建计划待办")
async def create_plan(
        data: schemas.Plan,
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await crud.PlanDal(auth.db).create_data(data))


@app.put("/plan/{data_id}", summary="更新计划待办状态")
async def put_plan(
        data_id: int,
        data: schemas.PlanUpdate,
        auth: Auth = Depends(AllUserAuth())
):
    data.update_user = auth.user.user_str
    return SuccessResponse(await crud.PlanDal(auth.db).put_data(data_id, data=data))


@app.delete("/plan", summary="批量删除计划待办", description="硬删除")
async def delete_plan(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.PlanDal(auth.db).delete_datas(ids=ids.ids)
    return SuccessResponse("删除成功")
